// Copyright (C) CompatibL Technologies LLC. All rights reserved.
// This code contains valuable trade secrets and may be used, copied,
// stored, or distributed only in accordance with a written license
// agreement and with the inclusion of this copyright notice. 

#region File Info
// File       : ChordArchitector.cs
// Description: ChordCreator.View.StringsFunctionality.ChordArchitector user control implementation.
// Package    : ChordCreator.View.StringsFunctionality 
//
#endregion

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ChordCreator.Model;
using ChordCreator.Services.ConfigurationServices;

namespace ChordCreator.View.StringsFunctionality
{
    /// <summary>
    /// Summary description for ChordArchitector.
    /// </summary>
    public partial class ChordArchitector : UserControl
    {
        private ChordScale chord_;
        private Dictionary<ScaleNote, List<CheckBox>> checkBoxes = new Dictionary<ScaleNote, List<CheckBox>>();

        /// <summary>
        /// Initializes a new instance of the <see cref="ChordArchitector"/> user control.
        /// </summary>
        public ChordArchitector()
        {
            InitializeComponent();
        }

        internal void Initialize(ChordScale chord)
        {
            chord_ = chord;
            Controls.Clear();
            CurrentConfiguration configuration = CurrentConfiguration.Instance;
            checkBoxes.Clear();
            int xOffset = 10;
            for (int i=0; i< chord.Notes.Count(); i++)
            {
                Label noteLabel = new Label();
                Controls.Add(noteLabel);
                ScaleNote note = chord.Notes[i];
                string noteLabelText = configuration.GetNotePresentation(note.Note);
                if (noteLabelText.Length > 1)
                {
                    noteLabel.Font = new Font(noteLabel.Font.FontFamily, 7);
                }
                noteLabel.Text = noteLabelText;
                noteLabel.Location = new Point(1 + xOffset, 4);
                noteLabel.Size = new Size(14, 13);
                List<CheckBox> checkBoxsList = new List<CheckBox>();
                checkBoxes.Add(note, checkBoxsList);
                int yOffset = 20;
                for (int j=0; j< HardcodeHelper.StringsCount; j++)
                {
                    CheckBox checkBox = new CheckBox();
                    checkBoxsList.Add(checkBox);
                    Controls.Add(checkBox);
                    checkBox.Location = new Point(1 + xOffset, yOffset);
                    checkBox.Click += checkBox_Click;
                    checkBox.Size = new Size(15, 14);
                    yOffset += 14;
                }
                yOffset += 3;
                Label posLabel = new Label();
                Controls.Add(posLabel);
                posLabel.Text = note.Degree.ToString();
                posLabel.Size = new Size(13, 13);
                posLabel.Location = new Point(1 + xOffset, yOffset);
                xOffset += 15;
            }
        }

        void checkBox_Click(object sender, EventArgs e)
        {
            Dictionary<int, List<ScaleNote>> stringAndNotes = new Dictionary<int, List<ScaleNote>>();
            for (int i = 1; i <= HardcodeHelper.StringsCount; i++ )
            {
                stringAndNotes.Add(i, new List<ScaleNote>());
            }
            foreach (KeyValuePair<ScaleNote, List<CheckBox>> pair in checkBoxes)
            {
                for (int i = 0; i < pair.Value.Count; i++)
                {
                    CheckBox checkBox = pair.Value[i];
                    if (checkBox.Checked)
                    {
                        stringAndNotes[i+1].Add(pair.Key);
                    }
                }
            }
            if (ChordArchitectureChanged != null)
            {
                ChordArchitectureChanged(this, new ChordArchChangedEventArgs() {Notes = stringAndNotes});
            }
        }

        internal event EventHandler<ChordArchChangedEventArgs> ChordArchitectureChanged; 
        internal class ChordArchChangedEventArgs : EventArgs
        {
            public Dictionary<int, List<ScaleNote>> Notes = new Dictionary<int, List<ScaleNote>>();
        }
    }
}
